import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_frame/app/components/custom_app_bar.dart';
import 'package:flutter_frame/app/core/utils/storage_util.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

class VideoPlayer extends StatefulWidget {
  const VideoPlayer({super.key});

  @override
  State<VideoPlayer> createState() => _VideoPlayerState();
}

class _VideoPlayerState extends State<VideoPlayer> {
  List _urlMap = [];

  late VideoPlayerController _videoPlayerController;
  ChewieController? _chewieController;
  int _currentVideoIndex = 0;

  TextEditingController _urlController = TextEditingController();

  @override
  void dispose() {
    _videoPlayerController.dispose();
    _chewieController?.dispose();
    _urlController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    getVideoUrl();
  }

  Future<void> _initializeVideoPlayer(String url) async {
    _videoPlayerController = VideoPlayerController.networkUrl(Uri.parse(url));
    await _videoPlayerController.initialize();
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      autoPlay: true,
      looping: false,
      isLive: true
    );
    setState(() {});
  }

  // 切换视频
  void changeVideo(int index) async {
    if (_currentVideoIndex == index) {
      return;
    }
    _currentVideoIndex = index;
    _chewieController?.dispose();
    await _videoPlayerController.dispose();
    _initializeVideoPlayer(_urlMap[index]['url'].toString());
  }

  // 保存播放链接到本地
  void saveVideoUrl() async {
    // 保存播放链接到本地
    StorageUtil.setString('key', jsonEncode(_urlMap));
  }

  // 获取播放链接
  void getVideoUrl() async {
    // 获取播放链接
    var url = StorageUtil.getString('key');
    if (url != null) {
      _urlMap = jsonDecode(url) as List;
      // 如果播放链接不为空，则初始化播放链接
      if (_urlMap.isNotEmpty) {
        _initializeVideoPlayer(_urlMap[0]['url'].toString());
      }
      setState(() {});
    } else {
      _urlMap.addAll([{'title': 'CCTV1', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.91:5999'},
          {'title': 'CCTV2', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.11:5999'},
          {'title': 'CCTV3', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.133:5999'},
          {'title': 'CCTV4', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.92:5999'},
          {'title': 'CCTV5', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.134:5999'},
          {'title': 'CCTV6', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.135:5999'},
          {'title': 'CCTV7', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.12:5999'},
          {'title': 'CCTV8', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.136:5999'},
          {'title': 'CCTV9', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.155:5999'},
          {'title': 'CCTV10', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.13:5999'},
          {'title': 'CCTV11', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.14:5999'},
          {'title': 'CCTV12', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.15:5999'},
          {'title': 'CCTV13', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.16:5999'},
          {'title': 'CCTV14', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.17:5999'},
          {'title': 'CCTV15', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.18:5999'},
          {'title': 'CCTV17', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.137:5999'},
          {'title': 'CCTV5+', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.185:5999'},
          {'title': '贵州移动', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.117:5999'},
          {'title': '贵州卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.1:5999'},
          {'title': '贵州2', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.2:5999'},
          {'title': '贵州3', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.3:5999'},
          {'title': '贵州4', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.4:5999'},
          {'title': '贵州5', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.5:5999'},
          {'title': '贵州6', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.6:5999'},
          {'title': '贵州7', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.9:5999'},
          {'title': '湖南卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.93:5999'},
          {'title': '浙江卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.100:5999'},
          {'title': '江苏卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.104:5999'},
          {'title': '安徽卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.101:5999'},
          {'title': '山东卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.97:5999'},
          {'title': '江西卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.94:5999'},
          {'title': '天津卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.99:5999'},
          {'title': '东方卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.98:5999'},
          {'title': '湖北卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.102:5999'},
          {'title': '吉林卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.95:5999'},
          {'title': '东南卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.103:5999'},
          {'title': '黑龙江卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.96:5999'},
          {'title': '四川卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.76:5999'},
          {'title': '河南卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.74:5999'},
          {'title': '重庆卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.78:5999'},
          {'title': '河北卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.77:5999'},
          {'title': '广东卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.59:5999'},
          {'title': '辽宁卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.69:5999'},
          {'title': '北京卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.81:5999'},
          {'title': '新疆卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.66:5999'},
          {'title': '山西卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.71:5999'},
          {'title': '陕西卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.72:5999'},
          {'title': '甘肃卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.73:5999'},
          {'title': '深圳卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.61:5999'},
          {'title': '青海卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.51:5999'},
          {'title': '内蒙古卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.53:5999'},
          {'title': '大湾区卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.60:5999'},
          {'title': '广西卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.64:5999'},
          {'title': '云南卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.63:5999'},
          {'title': '宁夏卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.75:5999'},
          {'title': '宁夏卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.75:5999'},
          {'title': '厦门卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.205:5999'},
          {'title': '厦门卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.205:5999'},
          {'title': '三沙卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.203:5999'},
          {'title': '农林卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.120:5999'},
          {'title': '海南卫视', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.58:5999'},
          {'title': '金鹰卡通', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.55:5999'},
          {'title': '哈哈炫动', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.56:5999'},
          {'title': '优漫卡通', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.57:5999'},
          {'title': '佳佳卡通', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.62:5999'},
          {'title': '贵阳-1', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.19:5999'},
          {'title': '贵阳-2', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.20:5999'},
          {'title': '贵阳-3', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.22:5999'},
          {'title': '六盘水-1', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.115:5999'},
          {'title': '六盘水-2', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.116:5999'},
          {'title': 'CCTV-风云音乐', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.24:5999'},
          {'title': 'CCTV-第一剧场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.25:5999'},
          {'title': 'CCTV-世界地理', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.26:5999'},
          {'title': 'CCTV-风云剧场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.28:5999'},
          {'title': 'CCTV-风云足球', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.29:5999'},
          {'title': 'CCTV-高尔夫-网球', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.223:5999'},
          {'title': 'CCTV-台球', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.224:5999'},
          {'title': 'CCTV-文化精品', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.32:5999'},
          {'title': 'CCTV-怀旧剧场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.33:5999'},
          {'title': 'CCTV-兵器科技', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.34:5999'},
          {'title': '国学频道', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.176:5999'},
          {'title': '环球奇观', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.43:5999'},
          {'title': '财富天下', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.186:5999'},
          {'title': '纪实人文', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.106:5999'},
          {'title': '纪实科教', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.107:5999'},
          {'title': '金鹰纪实', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.108:5999'},
          {'title': '法治天地', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.190:5999'},
          {'title': '快乐垂钓', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.191:5999'},
          {'title': '都市剧场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.194:5999'},
          {'title': '动漫秀场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.196:5999'},
          {'title': '游戏风云', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.197:5999'},
          {'title': '新视觉-剧场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.195:5999'},
          {'title': '欢笑剧场', 'url': 'http://www.qhjx.top:18000/udp/238.255.2.215:5999'}]);
      _initializeVideoPlayer(_urlMap[_currentVideoIndex]['url'].toString());

    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(
          titleText: '视频播放',
          actions: [
            Container(
              width: 200,
              child: CupertinoTextField(
                placeholder: '请输入播放链接',
                controller: _urlController,
              ),
            ),
            IconButton(onPressed: () async {
              if(_urlController.text.isNotEmpty) {
                var v = _urlController.text;
                setState(() {
                  _urlMap.insert(0, {'title': v, 'url': v});
                  saveVideoUrl();
                });
                _chewieController?.dispose();
                await _videoPlayerController.dispose();
                _currentVideoIndex = 0;
                _initializeVideoPlayer(v);
              }
            }, icon: const Icon(Icons.send)),
          ],
        ),
        body: Column(
          children: [
            SizedBox(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.width * 9 / 16,
              child: _chewieController != null &&
                  _chewieController!.videoPlayerController.value.isInitialized
                  ? Chewie(
                controller: _chewieController!,
              )
                  : const Center(
                child: CircularProgressIndicator(),
              ),
            ),
            Expanded(
                child: ListView.separated(
                  itemCount: _urlMap.length,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(_urlMap[index]['title'].toString()),
                      onTap: () {
                        changeVideo(index);
                      },
                    );
                  },
                  separatorBuilder: (context, index) {
                    return const Divider();
                  },
                ))
          ],
        ));
  }
}